<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit;
}

require_once MILEAGE_TRUST_PATH . '/class/AbstractEditAction.class.php';

/**
 * Mileage_RecEditAction
**/
class Mileage_RecEditAction extends Mileage_AbstractEditAction
{
	/**
	 * _getId
	 * 
	 * @param   void
	 * 
	 * @return  int
	**/
	protected function _getId()
	{
		return $this->mRoot->mContext->mRequest->getRequest('rec_id');
	}

	/**
	 * &_getHandler
	 * 
	 * @param   void
	 * 
	 * @return  Mileage_MileHandler
	**/
	protected function &_getHandler()
	{
		$handler =& $this->mAsset->getObject('handler', 'rec');
		return $handler;
	}

	/**
	* _setupActionForm
	* 
	* @param   void
	* 
	* @return  void
	**/
	protected function _setupActionForm()
	{
	// $this->mActionForm =& new Mileage_Admin_MileEditForm();
		$this->mActionForm =& $this->mAsset->getObject('form', 'rec', FALSE, 'edit');// isAdmin:FALSE
		$this->mActionForm->prepare();
	}

	/**
	 * hasPermission
	 * 
	 * @param   void
	 * 
	 * @return  bool
	**/
	public function hasPermission()
	{
		$root =& XCube_Root::getSingleton();
		$result = FALSE;

		// check user
		if (is_object($root->mContext->mXoopsUser)) {
			$mile_handler = $this->mAsset->getObject('handler', 'mile');
			$mile_id = intval($this->mRoot->mContext->mRequest->getRequest('mile_id'));
			$mile =& $mile_handler->getObjects(new Criteria('mile_id', $mile_id));
			$mile_url = $mile[0]->get('url');
			if (!empty($mile_url)) {
				// check ticket's permission
				if (strpos($mile_url, 'ticket') === 0 && $mile_url_ug = substr($mile_url, 7)) {
					$user_group_arr = explode('|', $mile_url_ug);
					if (count($user_group_arr) > 0) {
						$uid = $root->mContext->mXoopsUser->get('uid');
						$memberHandler =& Mileage_Utils::getXoopsHandler('member');
						$groupsByUid = $memberHandler->getGroupsByUser($uid);
						for ($i = 0; $i < count($uid_groupid_arr); $i++) {
							if (strpos($uid_groupid_arr[$i], 'u') === 0) {
								if (substr($uid_groupid_arr[$i], 1) == $uid) {
									$result = TRUE;
									break;
								}
							} else if (strpos($uid_groupid_arr[$i], 'g') === 0) {
								if (in_array(substr($uid_groupid_arr[$i], 1), $groupsByUid)) {
									$result = TRUE;
									break;
								}
							}
						}
					}
				} else {
					$result = TRUE;
				}
			}
		}

		return $result;
	}

	/**
	 * executeViewInput
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewInput(&$render)
	{
	//	$this->mRoot->mController->executeForward('./index.php?action=RecList');
		$this->mRoot->mController->executeForward(Mileage_Utils::makeUri($this->mAsset->mDirname, 'RecList'));
	}


	/**
	 * executeViewSuccess
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewSuccess(/*** XCube_RenderTarget ***/ &$render)
	{
		$access_uri = $this->mRoot->mContext->mRequest->getRequest('access_uri');
		$goToUrl = $this->mRoot->mContext->mRequest->getRequest('gotourl');
		if (!empty($access_uri)) {
			if (! Mileage_Utils::checkURI($access_uri)){
		//	$access_uri = preg_replace('/[^a-zA-Z0-9\/\+\.:_-]/', '', $access_uri);//TODO
		//	if (strpos($access_uri, XOOL_URL) !== 0) {
				$access_uri = XOOL_URL;
			}
			$this->mRoot->mController->executeForward($access_uri);
		} else if (!empty($goToUrl)) {
			$handler = $this->mAsset->getObject('handler', 'mile');
			$mile_id = intval($this->mRoot->mContext->mRequest->getRequest('mile_id'));
			$mile =& $handler->getObjects(new Criteria('mile_id', $mile_id));
			$this->mRoot->mController->executeForward($mile[0]->get('url'));
		} else {
		//	$this->mRoot->mController->executeForward('./index.php?action=RecList');
			$this->mRoot->mController->executeForward(Mileage_Utils::makeUri($this->mAsset->mDirname, 'RecList'));
		}
	}

	/**
	 * executeViewError
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewError(/*** XCube_RenderTarget ***/ &$render)
	{
//		$this->mRoot->mController->executeRedirect('./index.php?action=RecList', 1, _MD_MILEAGE_ERROR_DBUPDATE_FAILED);
		$this->mRoot->mController->executeRedirect(
				Mileage_Utils::makeUri($this->mAsset->mDirname, 'RecList'), 
				1, 
				_MD_MILEAGE_ERROR_DBUPDATE_FAILED
			);
	}

	/**
	 * executeViewCancel
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewCancel(/*** XCube_RenderTarget ***/ &$render)
	{
	//	$this->mRoot->mController->executeForward('./index.php?action=RecList');
		$this->mRoot->mController->executeForward(Mileage_Utils::makeUri($this->mAsset->mDirname, 'RecList'));
	}

}

